package com.woorigil.social.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.WebContentInterceptor;

import com.woorigil.social.Context;
import com.woorigil.social.DateUtils;
import com.woorigil.social.WebUtils;


public class BaseInterceptor extends WebContentInterceptor {
	private static Logger log = Logger.getLogger(BaseInterceptor.class);
	long startMill = 0;
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
		String uri = request.getRequestURI();
		startMill = System.currentTimeMillis();
		
		settingCookie(request, response);
		
		Context.Data data = new Context.Data();
		data.setUserAgent(request.getHeader("user-agent"));
		data.setRemoteAddr(request.getRemoteAddr());
		data.setLayout(getLayout(request));
		data.setExtention(StringUtils.substringAfterLast(uri, "."));
		data.setMobileDevice(WebUtils.isMobileDevice(request));
		data.setMainMenuId(NumberUtils.toInt(request.getParameter("mainMenuId"), 0));
		data.setSubMenuId(NumberUtils.toInt(request.getParameter("subMenuId"), 0));
		Context.set(data);
		return true;
	}

	private void settingCookie(HttpServletRequest request, HttpServletResponse response) {
		String sessionId = CookieUtils.getValue(request, "SESSION_ID");
		if (StringUtils.isBlank(sessionId)) {
			sessionId = String.valueOf(System.currentTimeMillis());
			CookieUtils.addCookie(response, "SESSION_ID", sessionId, DateUtils.ONE_YEAR);
		}
	}

	private String getLayout(HttpServletRequest request) {
		String layout = request.getParameter("layout");
		String url = request.getRequestURL().toString();
		
		if (StringUtils.isNotBlank(layout)) {
			return layout;
		}
		
		if (url.contains("/admin/")) {
			return "Admin";
		}
		
		if (url.contains("m.couponplay.kr")) {
			return "Mobile";
		}
		
		if (StringUtils.isBlank(layout) && WebUtils.isMobileDevice(request)) {
			return "Mobile";
		}
		
		return layout;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mav) throws Exception {
		if (handler instanceof AddCommonParamsAware) {
			mav.addObject("layout", Context.get().getLayout());
			mav.addObject("mainMenus", Context.getMainMenus());
			mav.addObject("navigationPath", Context.getNavigationPath());
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception exception) throws Exception {
//		System.out.println("time : " + (System.currentTimeMillis() - startMill));
		log.info((System.currentTimeMillis() - startMill) + "\t" +  request.getRequestURI() + "?" + request.getQueryString());
	}
}